package com.neusoft.neuiotms.dcom.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.QSchedulingRule;
import com.neusoft.neuiotms.dcom.model.SchedulingRule;
import com.neusoft.neuiotms.dcom.repository.CustomizedSchedulingRuleRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedSchedulingRuleRepositoryImpl extends AbstractRepository
        implements CustomizedSchedulingRuleRepository {

    @Override
    public List<SchedulingRule> index(final Searchable searchable) {
        final QSchedulingRule qSchedulingRule = QSchedulingRule.schedulingRule;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<SchedulingRule> query = this.query().selectFrom(qSchedulingRule);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<SchedulingRule> search(final Pageable pageable, final Searchable searchable) {
        final QSchedulingRule qSchedulingRule = QSchedulingRule.schedulingRule;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<SchedulingRule> query = this.query().selectFrom(qSchedulingRule);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return SchedulingRule.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QSchedulingRule qSchedulingRule = QSchedulingRule.schedulingRule;

        // 名称检索
        where.and(super.containsStr(qSchedulingRule.name, searchable, "name"));

        // 小组数
        where.and(super.equalsInt(qSchedulingRule.groupNumber, searchable, "groupNumber"));

        return where;
    }
}
